<template>
  <div class="major-wrap">
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-fa fa-graduation-cap"></i> 排课假期管理
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div class="container">
      <div class="query-form">
        <el-row :gutter="20">
          <el-col :span="2">
            <el-button @click="fetchData" icon="el-icon-plus"
              >获取本年度节假日信息</el-button
            >
          </el-col>
          <el-col :offset="13" :span="3">
            <el-input
              @keyup.enter.native="query"
              placeholder="日期信息"
              v-model="searchDate.date"
            />
          </el-col>

          <el-col :span="3">
            <el-input
              @keyup.enter.native="query"
              placeholder="假日信息"
              v-model="searchDate.message"
            />
          </el-col>
          <el-col :span="3">
            <el-button @click="query" icon="el-icon-search" type="primary"
              >搜索
            </el-button>
          </el-col>
        </el-row>
      </div>

      <!-- <el-row justify="center" type="flex">
        <el-pagination
          :current-page.sync="pageIndex"
          :page-size="pageSize"
          :total="pageSize * pageCount"
          @current-change="getPage"
          background
          layout="prev, pager, next"
        >
        </el-pagination>
      </el-row> -->

      <div class="table">
        <el-table :data="tableData" stripe>
          <el-table-column label="日历Id" prop="id" />
          <el-table-column label="日期" prop="date" />
          <el-table-column label="状态" prop="status" />
          <el-table-column label="假期信息" prop="message" />
          <el-table-column align="center" label="操作" width="200px">
            <template slot-scope="scope">
              <el-button @click="edit(scope.row.id)" size="mini" type="success"
                >编辑
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <el-dialog :visible.sync="editing" title="编辑" width="30%">
        <el-form :model="entityForm" label-width="70px" ref="form">
          <!-- ... 其他表单项 ... -->
          <el-form-item label="假日状态">
            <el-select
              placeholder="请选择:0工作日,1周末,2法定节假日,3节假日补班"
              v-model="entityForm.status"
              @change="updateMessage"
            >
              <el-option
                :key="index"
                :label="item.label"
                :value="item.value"
                v-for="(item, index) in statusList"
              />
            </el-select>
          </el-form-item>

          <!-- 将 v-model 绑定到 entityForm.message -->
          <el-form-item label="假日信息">
            <el-input v-model="entityForm.message"></el-input>
          </el-form-item>
        </el-form>
        <span class="dialog-footer" slot="footer">
          <el-button @click="save" type="primary">确 定</el-button>
          <el-button @click="editing = false">取 消</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import * as api from "../../api/admin/major";
// import { format, isValid } from "date-fns"; // 导入日期格式化函数

export default {
  name: "AdminMajor",
  data() {
    return {
      searchDate: {},
      entityForm: {},
      tableData: [],
      statusList: [
        { label: "工作日", value: 0 },
        { label: "周末", value: 1 },
        { label: "法定节假日", value: 2 },
        { label: "节假日补班", value: 3 },
        { label: "学校大型活动", value: 4 }
      ],

      editing: false
    };
  },
  methods: {
    updateMessage() {
      const selectedStatus = this.entityForm.status;
      const selectedOption = this.statusList.find(
        item => item.value === selectedStatus
      );
      if (selectedOption) {
        this.$message = selectedOption.label; // 更新 $message 属性
      }
    },
    //
    query() {
      if (this.searchDate.date) {
        api.getPage(this.searchDate.date).then(res => {
          this.tableData = res;
        });
      } else {
        api.getPage().then(res => {
          this.tableData = res;
        });
      }
    },

    // getPage() {
    //   api.getPage().then(res => {
    //     this.tableData = res;
    //   });
    // },
    fetchData() {
      api.create().then(() => {
        // 请求成功后再调用 getPage 方法获取页面数据
        this.query();
        this.$message.success("获取节假日成功");
      });
    },
    edit(id) {
      api.get(id).then(res => {
        this.entityForm = res;
        this.editing = true;
      });
    },
    save() {
      if (this.entityForm.id === -1) {
        api.create(this.entityForm).then(() => {
          this.finishSave();
        });
      } else {
        api.update(this.entityForm).then(() => {
          this.finishSave();
        });
      }
    },
    finishSave() {
      this.query();
      this.editing = false;
      this.$message.success("成功");
    }
  },
  created() {
    this.query();
  }
};
</script>

<style scoped></style>
